package ru.bragnikita.utils.swing.widget;

import org.apache.commons.lang3.ObjectUtils;
import ru.bragnikita.utils.swing.utils.ValueUtils;
import ru.bragnikita.utils.swing.widget.common.GuiEventSupport;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Nikita.Brazhnikov
 */
public class WtButton extends GuiEventSupport<String> implements CommandWidget {

	private String id;
	private String label;
	private JButton button;

	public WtButton(String id, String label, JButton button) {
		this.id = id;
		this.label = label;
		this.button = button;

		this.button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				fireEvent("click");
			}
		});
	}

	@Override
	public String getId() {
		return id;
	}

	@Override
	public String getAsString() {
		return label;
	}

	@Override
	public Object getValue() {
		return label;
	}

	@Override
	public void setValue(Object value) {
		label = ObjectUtils.toString(value, "");
	}

	@Override
	public void setValue(String param, Object value) {
		switch (param) {
			case "value":
				setValue(value);
				break;
			case "enabled":
				button.setEnabled(ValueUtils.getBoolean(value));
				break;
			default:
		}
	}
}
